package gentle.support.duplicateSubmission;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

//import com.fastaccess.permission.base.PermissionHelper;

/**
 * @author:wwz
 */
@Aspect
@Component
public class RepeatSubmitAspect {
    @Autowired
    private RedisUtils redisUtils;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final static Object PRESENT = new Object();

    @Pointcut("@annotation(gentle.support.duplicateSubmission.RejectMultiPost)")
    public void pointcut() {
    }

    private final static class Holder {
        private static HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }


    @Around("pointcut()&& @annotation(rejectMultiPost)")
    public Object around(ProceedingJoinPoint joinPoint, RejectMultiPost rejectMultiPost) throws Throwable {
        String key = Holder.request.getRequestURI();
        boolean lock = true;
        try {
            //设置解锁key
            lock = ResubmitLock.getInstance().lock(key, PRESENT);
            if (lock) {
                //放行
                return joinPoint.proceed(joinPoint.getArgs());
            } else {
                //响应重复提交异常
                logger.error("重复提交【代码未执行】");
                System.out.println("重复提交【代码未执行】");
                return "响应重复提交异常";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return joinPoint.proceed(joinPoint.getArgs());
        } finally {
            //设置解锁key和解锁时间
            ResubmitLock.getInstance().unLock(lock, key, 5);
        }
    }
//        String key = null;
//        try {
////            key = ResubmitLock.handleKey(DateTimeKit.now());
////            key = PermissionHelper.currentUserSession().getUserId()+Holder.request.getRequestURI();
//            key = Holder.request.getRequestURI();
//            //这里是redis+lua。一个key在指定时间内。只获取一次。不会出现插队
//            if (redisUtils.lock(key, key, rejectMultiPost.timeout())) {
//                return joinPoint.proceed(joinPoint.getArgs());
//            } else {
//                logger.error("重复提交【代码未执行】");
//                System.out.println("重复提交【代码未执行】");
//            }
//        } catch (Throwable throwable) {
//            logger.error(throwable.getMessage());
//            throwable.printStackTrace();
//            if (key == null && "null".equals(key)) {
//                redisUtils.unLock(key, key);
//            } else {
//                try {
//                    return joinPoint.proceed(joinPoint.getArgs());
//                } catch (Throwable throwable1) {
//                    logger.error(throwable1.getMessage());
//                    throwable1.printStackTrace();
//                }
//            }
//
//        }
//        return JsonResult.SUCCESS;

}